"use strict";
cc._RF.push(module, '83e71BFtMFHUZ8vefL8Yxl5', 'socket.io');
// resources/script/lib/socket.io.js

"use strict";

cc.Class({
    extends: cc.Component,

    properties: {},

    connect: function connect(url, options) {
        var self = this;
        this.ws = new WebSocket(url + "?userid=" + cc.qipai.user.id);
        this.ws.onopen = function (event) {
            console.log("Send Text WS was opened.");
        };
        this.ws.onmessage = function (event) {
            var data = self.parse(event.data);
            if (data != null && data.event != null) {
                cc.qipai.event[data.event](event.data);
            }
            console.log("response text msg: " + event.data);
        };
        this.ws.onerror = function (event) {
            console.log("Send Text fired an error");
        };
        this.ws.onclose = function (event) {
            console.log("WebSocket instance closed.");
        };
        return this;
    },
    on: function on(command, func) {
        cc.qipai.event[command] = func;
    },
    exec: function exec(command, data) {
        if (this.ws.readyState === WebSocket.OPEN) {
            data.command = command;
            data.userid = cc.qipai.user.id;
            data.orgi = cc.qipai.user.orgi;
            data.token = cc.qipai.authorization;
            this.ws.send(JSON.stringify(data));
        }
    },
    emit: function emit(command, data) {
        var param = {
            data: data
        };
        this.exec(command, param);
    },
    disconnect: function disconnect() {},
    parse: function parse(result) {
        return JSON.parse(result);
    }
});

cc._RF.pop();